#!/usr/bin/env python3
# -*- coding: utf-8 -*-


__author__ = 'Minho Lee'

import random
from config import logging
import requests
from urllib import parse
from bs4 import BeautifulSoup


class Spider(object):

    def __init__(self):
        self.headers = {
            'User-Agent': r'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.84 Safari/537.36',
            'Referer': r'https://www.baidu.com/s?wd=%E5%B0%8F%E8%AF%B4&rsv_spt=1',
            'Accept': r'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8',
            'Connection': 'keep-alive',
            'Accept-Language':'zh-CN,zh;q=0.9,en;q=0.8,zh-TW;q=0.7',
            'Accept-Encoding':'gzip, deflate'
        }
        self.user_agent_list = [
            "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.1 (KHTML, like Gecko) Chrome/22.0.1207.1 Safari/537.1",
            "Mozilla/5.0 (X11; CrOS i686 2268.111.0) AppleWebKit/536.11 (KHTML, like Gecko) Chrome/20.0.1132.57 Safari/536.11",
            "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/536.6 (KHTML, like Gecko) Chrome/20.0.1092.0 Safari/536.6",
            "Mozilla/5.0 (Windows NT 6.2) AppleWebKit/536.6 (KHTML, like Gecko) Chrome/20.0.1090.0 Safari/536.6",
            "Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.1 (KHTML, like Gecko) Chrome/19.77.34.5 Safari/537.1",
            "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/536.5 (KHTML, like Gecko) Chrome/19.0.1084.9 Safari/536.5",
            "Mozilla/5.0 (Windows NT 6.0) AppleWebKit/536.5 (KHTML, like Gecko) Chrome/19.0.1084.36 Safari/536.5",
            "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/536.3 (KHTML, like Gecko) Chrome/19.0.1063.0 Safari/536.3",
            "Mozilla/5.0 (Windows NT 5.1) AppleWebKit/536.3 (KHTML, like Gecko) Chrome/19.0.1063.0 Safari/536.3",
            "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_0) AppleWebKit/536.3 (KHTML, like Gecko) Chrome/19.0.1063.0 Safari/536.3",
            "Mozilla/5.0 (Windows NT 6.2) AppleWebKit/536.3 (KHTML, like Gecko) Chrome/19.0.1062.0 Safari/536.3",
            "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/536.3 (KHTML, like Gecko) Chrome/19.0.1062.0 Safari/536.3",
            "Mozilla/5.0 (Windows NT 6.2) AppleWebKit/536.3 (KHTML, like Gecko) Chrome/19.0.1061.1 Safari/536.3",
            "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/536.3 (KHTML, like Gecko) Chrome/19.0.1061.1 Safari/536.3",
            "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/536.3 (KHTML, like Gecko) Chrome/19.0.1061.1 Safari/536.3",
            "Mozilla/5.0 (Windows NT 6.2) AppleWebKit/536.3 (KHTML, like Gecko) Chrome/19.0.1061.0 Safari/536.3",
            "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/535.24 (KHTML, like Gecko) Chrome/19.0.1055.1 Safari/535.24",
            "Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/535.24 (KHTML, like Gecko) Chrome/19.0.1055.1 Safari/535.24"
        ]
        self.session = requests.Session()
        self.cookies = {}

    def request(self, url):
        """请求远程URL"""
        ua = random.choice(self.user_agent_list)  # 从user_agent_list中随机抽取出一个字符串
        self.headers['User-Agent'] = ua

        ##cookies = {'incap_ses_199_1454401':'hRC2CipdoxuMuZXMEv7CArBYUFoAAAAAIwf94e4TimYP5VL8uUgw2g==','UM_distinctid':'160b14ab3cd64b-088b2225e7be8-e323462-1fa400-160b14ab3ceb8d','visid_incap_1454401':'aNtdhD3qSGCI0aMQfB7xJAUPSloAAAAAQUIPAAAAAACClNqa2hhNCPTUqDHIzPgd'}
        req = self.session.get(url, headers=self.headers,cookies=self.cookies)

        res = req.text.encode(req.encoding)
        print(res)
        # 如果返回值不是200,表示请求失败
        if req.status_code != 200:
            logging.critical('远程响应失败：' + req.status_code)
            return None
        return res

    def set_cookies(self, cookies):
        self.cookies = cookies

    def resolve_catalog(self, url):
        """解析小说目录,需要继承类实现"""

    def resolve_fiction_content(self, url):
        """解析小说内容,需要继承类实现"""

    def resolve_fiction_metadata(self, url):
        """解析小说元数据"""
